package com.small.act.ecard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.small.R;
import com.small.adapter.DiscountAdapter;
import com.small.commom.Action;
import com.small.interfaces.Task;
import com.small.interfaces.Util;
import com.small.model.DiscountListModel;
import com.small.model.SQChildModel;
import com.small.model.SQParentModel;
import com.small.model.ShopChildModel;
import com.small.model.ShopParentModel;
import com.small.view.custview.dialog.MyPopMenu;

public class ElectronicHomeActivity extends Activity implements Task{
	
	ListView ele_list_view;
	EditText ele_txt_find;
	ImageButton btn_ele_search, btn_ele_back, bottom_ele_yhxx, bottom_ele_gps, bottom_ele_mysc;
	LinearLayout ele_layout_gps;
	Button ele_btn_region, ele_btn_type, ele_btn_order;
	
	ArrayList<String> ele_list_Shopfirst_name = null;
	ArrayList<String> ele_list_Shopsecond_name = null;
	ArrayList<String> ele_list_SQfirst_name = null;
	ArrayList<String> ele_list_SQsecond_name = null;
	ArrayList<String> ele_list_second_ordername = null;
	MyPopMenu ele_pop;
	Context ele_ctx;
	Action ele_action;
	List<ShopParentModel> ele_shoplist;
	List<SQParentModel> ele_sqlist;
	List<ShopChildModel> ele_childShopList;
	List<SQChildModel> ele_childSQList;
	private final int ELE_SHOP_TYPE_FIRST = 0;
	private final int ELE_SHOP_TYPE_SECOND = 1;
	private final int ELE_SQ_TYPE_FIRST = 2;
	private final int ELE_SQ_TYPE_SECOND = 3;
	private final int ELE_ORDER_TAG = 4;
	
	 private HashMap<String, Object> ele_favDiscountMap = null;
	  List<DiscountListModel> ele_discountList = null;
	  DiscountAdapter ele_list_adapter;
	  private int ele_result;
	  
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_electronic_home_view);
	findView();
	initData();
	
	}
		private void initData() {
		// TODO Auto-generated method stub
		Util.removeActivity();
		Util.addActivity(this);
		ele_ctx = ElectronicHomeActivity.this;
		ele_action = new Action(ele_ctx);
		Resources res = ele_ctx.getResources();
		ele_list_second_ordername = new ArrayList<String>();
		ele_list_second_ordername.add(res.getString(R.string.nearest));
		ele_list_second_ordername.add(res.getString(R.string.latest));
		ele_list_second_ordername.add(res.getString(R.string.welcome));

		//ele_action.showProgressDialog("正在加载...");
		//new AsyncDataLoader(ele_asyncCallback).execute();
		
		
	}
	





	private void findView() {
		// TODO Auto-generated method stub
		

		ele_btn_region = (Button) this.findViewById(R.id.yhq_gps_sq);
		ele_btn_type = (Button) this.findViewById(R.id.yhq_gps_type);
		ele_btn_order = (Button) this.findViewById(R.id.yhq_gps_byme);
		ele_btn_order.setOnClickListener(onClick);
		ele_btn_type.setOnClickListener(onClick);
		ele_btn_region.setOnClickListener(onClick);
//		btn_ele_search = (ImageButton) this.findViewById(R.id.ele_yhq_find_btn_on);
		btn_ele_back = (ImageButton) this.findViewById(R.id.btn_home);
//		btn_ele_search.setOnClickListener(onClick);
		btn_ele_back.setOnClickListener(onClick);

	    //ele_list_view = (ListView) this.findViewById(R.id.yhqhome_list);
		//ele_list_view.setOnItemClickListener(new OnItemClickListener() {

			//@Override
			//public void onItemClick(AdapterView<?> arg0, View view,
					//int position, long arg3) {
			//	startActivity(new Intent(ElectronicHomeActivity.this,
					//	DiscountDetailActivity.class));
			//}
			
		//});
		

		
		
		
		// list_ada=new YhqHome_listAda(this);
		// list_view.setAdapter(list_ada);
		//
		// layout_gps=(LinearLayout)this.findViewById(R.id.yhq_gps_layout);
		// txt_find=(EditText)this.findViewById(R.id.yhq_find_txt);
		// bottom_yhxx=(ImageButton)this.findViewById(R.id.buttom_yhxx);
		// bottom_gps=(ImageButton)this.findViewById(R.id.bottom_gps);
		// bottom_mysc=(ImageButton)this.findViewById(R.id.bottom_myyhq);
		// bottom_yhxx.setOnClickListener(onClick);
		// bottom_gps.setOnClickListener(onClick);
		// bottom_mysc.setOnClickListener(onClick);
	}
	   /* private Callback ele_asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {
			 
			// action.getFavDiscountInfo(Common.model.getMemberID(), 2, 1);
			ele_favDiscountMap = ele_action.getDiscountInfobydistance(1, 1, "1", 5, 1,
					117.15863, 36.65231, 10000, "运动", "休闲");

		}
		

		@Override
		public void onPrepare() {
		}

		
		
		public void onFinish() {

			ele_action.stopProgressDialog();

			if (ele_favDiscountMap != null) {
				Integer i = (Integer) ele_favDiscountMap.get("ele_result");
				ele_result = (int) i;
				Log.d("				ele_result", "" + ele_result);
				ele_discountList = (List<DiscountListModel>) ele_favDiscountMap.get("ele_discountList");

				resultMethod(ele_result, ele_discountList);
			} else {
				resultMethod(-2, null);
			}

		}
	};
	

	protected void resultMethod(int result2,
			List<DiscountListModel> mydiscountlist) {
		switch (result2) {
		case 0:
			ele_action.showShortToast("请求失败，请稍候再试...");
			break;
		case 1:
			if (mydiscountlist != null) {
				ele_list_adapter = new DiscountAdapter(ElectronicHomeActivity.this,
						mydiscountlist);
				ele_list_view.setAdapter(ele_list_adapter);
			} else {
				ele_action.showShortToast("请求失败，请稍候再试...");
			}
			break;
		case -2:
			ele_action.showShortToast("数据异常，请求失败...");
			break;

		default:
			break;
		}
	}
	*/
	
	OnClickListener onClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()){
			case R.id.btn_home:
				finish();
				break;
			case R.id.yhq_gps_sq:
				eleregionDialog(ele_btn_region);//商圈对话框
				break;
			case R.id.yhq_gps_type:
				elefilterCategory(ele_btn_type);//类别对话框
				break;
			case R.id.yhq_gps_byme:
				elefilterOrder(ele_btn_order);//paixu排序，你看看对不对，我注释的   是这样的  。
				//然后你是点哪个的时候报错？类别
				break;
		/*  case R.id.yhq_find_btn_on:
				// String txtFind = txt_find.getText().toString().trim();
				// if (!txtFind.equals("")) {
				// intFindType = 3;// 查询方式 关键字搜索 =3
				// strFindKey = txtFind;
				// isLoad = true;
				// // onData();
				// } else {
				// txt_find.setError("请输入");
				// }
				break;
			 
			/*case R.id.ele_bottom_gps:
				ele_layout_gps.setVisibility(View.VISIBLE);

				break;
			case R.id.bottom_mysc:
				Intent i = new Intent(ElectronicHomeActivity.this,
						FavDiscountActivity.class);
				ElectronicHomeActivity.this.startActivity(i);
				break;
				*/
			
			}
			
		}
	}; 
	private void elefilterCategory(View view) {
		
		ele_shoplist = ele_action.getParentShopInfo();
		
		if(ele_shoplist != null){
			ele_list_Shopfirst_name = new ArrayList<String>();
			for(ShopParentModel sqM : ele_shoplist){
				ele_list_Shopfirst_name.add(sqM.getCategoryName());
				
			}
			ele_list_Shopsecond_name = ele_childShoplist(ele_shoplist.get(0).getCategoryID());
			
			Log.e("ffffsdfsfsfsfsf》》》》", String.valueOf(ele_list_Shopsecond_name));
			if (ele_list_Shopsecond_name==null ) {
				ele_pop = new MyPopMenu(ElectronicHomeActivity.this, ele_list_Shopfirst_name,
						ele_list_Shopsecond_name);//这个就是那个筛选对话框
				ele_pop.initPopupWindow(R.drawable.filter_mid_bg,
						R.layout.choice_single_listpop_layout, ELE_SHOP_TYPE_FIRST,
						ELE_SHOP_TYPE_SECOND);
				ele_pop.show(view);
				ele_pop.refresh(0,0, ele_list_SQsecond_name);
			}else{
				ele_action.showShortToast("为空，弹个毛");
			}
			
			
		} else{
			ele_action.showShortToast("为空，弹个毛");
		}
	}
	private ArrayList<String> ele_childShoplist(int id) {
		// TODO Auto-generated method stub
		ArrayList<String> ele_lists = null;
		ele_childShopList = ele_action.getChildShopInfo(id);
		if(ele_childShopList != null){
			ele_lists = new ArrayList<String>();
			for(ShopChildModel sqCM : ele_childShopList){
				ele_lists.add(sqCM.getCategoryName());
			}
		}
		return ele_lists;
	}
	// public void ShowFindType() {
		// final CharSequence[] items = { "离我最近", "人气最高", "最新发布" };
		// AlertDialog dlg = new AlertDialog.Builder(DiscountHomeActivity.this)
		// .setTitle("排序方式")
		// .setItems(items, new DialogInterface.OnClickListener() {
		// public void onClick(DialogInterface dialog, int item) {
		// intOrderType = item + 1;
		// btn_order.setText(items[item]);
		// // onData();
		// }
		// }).create();
		// dlg.show();
		// }

		// public void ShowFindJuli() {
		// final CharSequence[] items = { "500M", "1000M", "2000M", "3000M" };
		// AlertDialog dlg = new AlertDialog.Builder(DiscountHomeActivity.this)
		// .setTitle("请选择")
		// .setItems(items, new DialogInterface.OnClickListener() {
		// public void onClick(DialogInterface dialog, int item) {
		// switch (item) {
		// case 0:
		// intJuli = 500;
		// break;
		// case 1:
		// intJuli = 1000;
		// break;
		// case 2:
		// intJuli = 2000;
		// break;
		// case 3:
		// intJuli = 3000;
		// break;
		// }
		// btn_region.setText(items[item]);
		// // onData();
		// }
		// }).create();
		// dlg.show();
		// }
	@Override
	public void onRefresh(int page, HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		switch (page){
		case ELE_SHOP_TYPE_FIRST:
			Integer i = (Integer)map.get("position");
			Log.e("000", "" + i);
			ele_list_Shopsecond_name  = ele_childShoplist(ele_shoplist.get((int) i)
					.getCategoryID());
			ele_pop.refresh(0,0, ele_list_Shopsecond_name );
			break;
		case ELE_SHOP_TYPE_SECOND:
			Integer i2 = (Integer) map.get("position");
			Log.e("111", "您选择了第" + i2 + ele_list_Shopsecond_name .get(i2));
			Toast.makeText(ele_ctx, "您选择了第" + i2 + ele_list_Shopsecond_name .get(i2),
					Toast.LENGTH_SHORT).show();
			ele_pop.dismiss();
			break;
		case ELE_SQ_TYPE_FIRST:
			Integer i3 = (Integer) map.get("position");
			Log.e("000", "" + i3);
			ele_list_SQsecond_name  = ele_childSQlist(ele_sqlist.get((int) i3)
					.getCategoryID());
			ele_pop.refresh(0,0, ele_list_SQsecond_name );
			break;
		case ELE_SQ_TYPE_SECOND:
			Integer i4 = (Integer) map.get("position");
			Log.e("333", "您选择了第" + i4 + ele_list_SQsecond_name .get(i4));
			Toast.makeText(ele_ctx, "您选择了第" + i4 + ele_list_SQsecond_name .get(i4),
					Toast.LENGTH_SHORT).show();
			ele_pop.dismiss();
			break;
		case ELE_ORDER_TAG:
			Integer i5 = (Integer) map.get("position");
			Log.e("333", "您选择了第" + i5 + ele_list_second_ordername.get(i5));
			Toast.makeText(ele_ctx, "您选择了第" + i5 + ele_list_second_ordername.get(i5),
					Toast.LENGTH_SHORT).show();
			ele_pop.dismiss();
			break;
			
		}
		
	}
	private ArrayList<String> ele_childSQlist(int id) {
		ArrayList<String> ele_lists = null;
		ele_childSQList = ele_action.getChildSQInfo(id);
		if (ele_childSQList != null) {
			ele_lists = new ArrayList<String>();
			for (SQChildModel sqCM : ele_childSQList) {
				ele_lists.add(sqCM.getCategoryName());
			}
		}
		return ele_lists;
	}


	protected void elefilterOrder(View view) {
		ele_pop = new MyPopMenu(ElectronicHomeActivity.this, null,
				ele_list_second_ordername);
		ele_pop.initPopupWindow(R.drawable.filter_right_bg,
				R.layout.choice_single_listpop_layout, -1, ELE_ORDER_TAG);
		ele_pop.show(view);
	}

	

	private void eleregionDialog(View view) {

		ele_sqlist = ele_action.getParentSQInfo();

		if (ele_sqlist != null) {
			ele_list_SQfirst_name  = new ArrayList<String>();
			for (SQParentModel sqM : ele_sqlist) {
				ele_list_SQfirst_name .add(sqM.getCategoryName());
			}
			ele_list_SQsecond_name  = ele_childSQlist(ele_sqlist.get(0).getCategoryID());
			ele_pop = new MyPopMenu(ElectronicHomeActivity.this, ele_list_SQfirst_name ,ele_list_SQsecond_name );
			ele_pop.initPopupWindow(R.drawable.filter_left_bg,
					R.layout.choice_single_listpop_layout, ELE_SQ_TYPE_FIRST,
					ELE_SQ_TYPE_SECOND);
			ele_pop.show(view);
			ele_pop.refresh(0,0, ele_list_SQsecond_name );
		} 
		 
		
		
	}
	

	
	
	
}